<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="年龄" prop="age">
        <el-input
          v-model="queryParams.age"
          placeholder="请输入年龄"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="介绍" prop="introduce">
        <el-input
          v-model="queryParams.introduce"
          placeholder="请输入介绍"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="城市" prop="province">
        <el-input
          v-model="queryParams.province"
          placeholder="请选择城市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="市" prop="municipality">
        <el-input
          v-model="queryParams.municipality"
          placeholder="请输入市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区" prop="county">
        <el-input
          v-model="queryParams.county"
          placeholder="请输入区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="详情地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入详情地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="QQ" prop="qq">
        <el-input
          v-model="queryParams.qq"
          placeholder="请输入QQ"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信" prop="wechat">
        <el-input
          v-model="queryParams.wechat"
          placeholder="请输入微信"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:users:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:users:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:users:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:users:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="usersList"
      @selection-change="handleSelectionChange"
      @row-dblclick="messageInfo"
    >    
    <el-table-column type="expand">
      <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="介绍">
              <span>{{ props.row.introduce }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="名字" align="center" prop="name" />
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="介绍" align="center" prop="introduce" show-overflow-tooltip />
      <el-table-column label="省份" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="municipality" />
      <el-table-column label="区" align="center" prop="county" />
      <el-table-column label="详情地址" align="center" prop="address" show-overflow-tooltip/>
      <el-table-column label="QQ" align="center" prop="qq" />
      <el-table-column label="微信" align="center" prop="wechat" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="图片" align="center" prop="img" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.img" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="服务" align="center" prop="services" show-overflow-tooltip/>
      <el-table-column label="备注" align="center" prop="remarks" show-overflow-tooltip/>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column >
      <el-table-column label="操作" align="center" width="150" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:users:edit']"
            >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:users:remove']"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户信息对话框 -->
    <!-- 添加或修改客户信息对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1000px"
      append-to-body
    >
      <el-row :gutter="15">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-col :span="12">
            <el-form-item label="名字" prop="name">
              <el-input v-model="form.name" placeholder="请输入名字" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="价格" prop="price">
              <el-input v-model="form.price" placeholder="请输入价格" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
          <el-form-item label="市" prop="municipality">
            <el-input v-model="form.municipality" placeholder="请输入市" />
          </el-form-item>
          </el-col>
          <el-form-item label="区" prop="county">
            <el-input v-model="form.county" placeholder="请输入区" />
          </el-form-item> -->
          <el-col :span="12">
            <el-form-item label="QQ" prop="qq">
              <el-input v-model="form.qq" placeholder="请输入QQ" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="微信" prop="wechat">
              <el-input v-model="form.wechat" placeholder="请输入微信" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入电话" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="城市" prop="province">
              <!-- <el-input v-model="form.province" placeholder="请选择城市" /> -->
              <el-cascader
                placeholder="请选择城市"
                ref="cascaderArr"
                size="large"
                :options="options"
                v-model="selectedOptions"
                filterable
                clearable
              >
              </el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="详细地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入详细地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年龄" prop="age">
              <el-input v-model="form.age" placeholder="请输入年龄" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服务" prop="services">
              <el-input v-model="form.services" placeholder="请输入服务" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remarks">
              <el-input v-model="form.remarks" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="图片">
              <image-upload v-model="form.img" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="文档" prop="field10">
              <file-upload v-model="form.field10" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="介绍" prop="introduce">
              <el-input
                v-model="form.introduce"
                type="textarea"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listUsers,
  getUsers,
  delUsers,
  addUsers,
  updateUsers,
} from "@/api/system/users";
import { regionDataPlus } from "element-china-area-data";
import { provinceAndCityData } from "element-china-area-data";

import { regionData } from 'element-china-area-data'

export default {
  name: "Users",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户信息表格数据
      usersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        age: null,
        price: null,
        introduce: null,
        province: null,
        municipality: null,
        county: null,
        address: null,
        qq: null,
        wechat: null,
        phone: null,
        img: null,
        services: null,
        remarks: null,
        field1: null,
        field2: null,
        field3: null,
        field4: null,
        field5: null,
        field6: null,
        field7: null,
        field8: null,
        field9: null,
        field10: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: "名字不能为空", trigger: "blur" }],
      },
      options: regionData,
      selectedOptions: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客户信息列表 */
    getList() {
      this.loading = true;
      listUsers(this.queryParams).then((response) => {
        this.usersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        age: null,
        price: null,
        introduce: null,
        province: null,
        municipality: null,
        county: null,
        address: null,
        qq: null,
        wechat: null,
        phone: null,
        img: null,
        services: null,
        remarks: null,
        field1: null,
        field2: null,
        field3: null,
        field4: null,
        field5: null,
        field6: null,
        field7: null,
        field8: null,
        field9: null,
        field10: null,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getUsers(id).then((response) => {
        debugger
        this.form = response.data;
        let selected1 = JSON.stringify(response.data.field1);
        let splitData = selected1.split(",");
        let cleanData = splitData.map(item => item.replace(/"/g, '')); // 移除双引号

        let arr = [];
        //11,1101,110106
        arr.push("11");
        arr.push("1101");
        arr.push("110106");
        // arr.push(selected1.substring(1, 7));
        // arr.push(selected1.substring(8, 14));
        // arr.push(selected1.substring(15, 21).length == 1 ? '' : selected1.substring(15, 21));
        console.log(cleanData);
        this.selectedOptions = cleanData; //重要，把处理后的数据重新赋值给selectOptions，让其显示
        this.open = true;
        this.title = "修改客户信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      const checkedNodes = this.selectedOptions;
      console.log(checkedNodes);
      debugger
      const checkedNode = this.$refs["cascaderArr"].getCheckedNodes();
      // console.log( checkedNode[0].data.label ) ;  //获取当前点击节点的label值
      // console.log( checkedNode[0].pathLabels) ;  //获取由label组成的数组
      if (checkedNode.length > 0) {
        // this.form.province = checkedNode[0].pathLabels[0];
        // this.form.municipality = checkedNode[0].pathLabels[1];
        // this.form.county = checkedNode[0].pathLabels[2];
        const { pathLabels } = checkedNode[0];
        this.form.province = pathLabels[0];
        this.form.municipality = pathLabels[1];
        this.form.county = pathLabels[2];
      }
      this.form.field1 = checkedNodes[0] + "," + checkedNodes[1] + "," + checkedNodes[2];
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateUsers(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUsers(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除客户信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delUsers(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/users/export",
        {
          ...this.queryParams,
        },
        `users_${new Date().getTime()}.xlsx`
      );
    },
    messageInfo(row) {
      this.$router.push({ path: "usersdetails", query: { id: row.id } });
    },
  },
};
</script>
